﻿
using System.Collections.Generic;
using System;
using BCService.Utils;
using BCCommon;
using BCData.TMS.TransportSpecificServiceConfig;
using BCDto.TMS.TransportSpecificServiceConfig;
using BCEntity.TMS.TransportSpecificServiceConfig;
using BCDto.Sim.Admin.Admin;
using BCEntity.TMS.TransportSpecificServiceConfigOverWrite;
using BCEntity.Common.EntityNameConst;
using BCData.TMS.TransportSpecificServiceConfigOverWrite;
using BCData.TMS.Agent.AgentRecord;
using BCDto.TMS.FreightContact;
using BCDto.Website.User;
using BCData.TMS.Agent.AgentAreaRecord;
using BCService.Common.Area;
using System.Linq;
using BCDto.TMS.TransportSpecificServiceConfigOverWrite;
using BCService.TMS.VehicleTypeService;
using BCData.TMS.VehicleType;
using System.Security.Cryptography.X509Certificates;

namespace BCService.TMS.TransportSpecificServiceConfig
{
    public class TransportSpecificServiceConfigService : ITransportSpecificServiceConfigService
    {
        #region var
        private readonly IDatabaseContext databaseContext;
        private ITransportSpecificServiceConfigData transportSpecificServiceConfigData;
        private ITransportSpecificServiceConfigOverWriteData transportSpecificServiceConfigOverWriteData;
        private readonly IAgentRecordData agentRecordData;
        private readonly IAgentAreaRecordData agentAreaRecordData;
        private readonly IRedisService redisService;
        private readonly IZoneService zoneService;
        private readonly IVehicleTypeData vehicleTypeData;
        private readonly string key = string.Format("{0}", EntityNameConst.TransportSpecificServiceConfigEntity);
        private readonly string keyView = string.Format("{0}", EntityNameConst.TransportSpecificServiceConfigViewEntity);

        #endregion

        #region 构造函数


        public TransportSpecificServiceConfigService(IDatabaseContext databaseContext,
            ITransportSpecificServiceConfigData transportSpecificServiceConfigData,
            ITransportSpecificServiceConfigOverWriteData transportSpecificServiceConfigOverWriteData,
            IAgentRecordData agentRecordData,
            IRedisService redisService,
            IAgentAreaRecordData agentAreaRecordData,
             IVehicleTypeData vehicleTypeData,
            IZoneService zoneService)
        {
            this.databaseContext = databaseContext;
            this.transportSpecificServiceConfigData = transportSpecificServiceConfigData;
            this.transportSpecificServiceConfigOverWriteData = transportSpecificServiceConfigOverWriteData;
            this.agentRecordData = agentRecordData;
            this.redisService = redisService;
            this.agentAreaRecordData = agentAreaRecordData;
            this.zoneService = zoneService;
            this.vehicleTypeData = vehicleTypeData;
        }
        #endregion

        #region Add
        public TransportSpecificServiceConfigDto Add(TransportSpecificServiceConfigRequestDto transportSpecificServiceConfigRequestDto, AdminDto admin)
        {
            if (admin.CompanyId == 1)
            {
                var vehicleTypeEntity = vehicleTypeData.GetByIdAsync(transportSpecificServiceConfigRequestDto.VehicleTypeId).GetAwaiter().GetResult() ?? throw new ArgumentException("找不到指定车型!");
                return this.transportSpecificServiceConfigData.InsertAsync(new TransportSpecificServiceConfigEntity()
                {
                    VehicleTypeId = vehicleTypeEntity.VehicleTypeId,
                    VehicleTypeName = vehicleTypeEntity.VehicleTypeName,
                    SpecificServiceType = transportSpecificServiceConfigRequestDto.SpecificServiceType,
                    ServiceTitle = transportSpecificServiceConfigRequestDto.ServiceTitle,
                    ShortName = transportSpecificServiceConfigRequestDto.ShortName,
                    HasRemark = transportSpecificServiceConfigRequestDto.HasRemark,
                    ServiceRemark = transportSpecificServiceConfigRequestDto.ServiceRemark,
                    ServiceAmount = transportSpecificServiceConfigRequestDto.ServiceAmount,
                    HasUnit = transportSpecificServiceConfigRequestDto.HasUnit,
                    ServiceUnit = transportSpecificServiceConfigRequestDto.ServiceUnit,
                    CreateAdminId = admin.AdminId,
                    CreateAdminName = admin.FullName,
                    CreateTime = DateTime.Now,
                    EditAdminId = admin.AdminId,
                    EditAdminName = admin.FullName,
                    UpdateTime = DateTime.Now,
                }).GetAwaiter().GetResult().As<TransportSpecificServiceConfigDto>();
            }
            else
            {
                throw new ArgumentException("代理不能添加");
            }
        }
        #endregion

        #region update
        public TransportSpecificServiceConfigViewDto Update(TransportSpecificServiceConfigPutDto transportSpecificServiceConfigPutDto, AdminDto admin)
        {
            if (admin.AdminId == 1)
            {
                var vehicleTypeEntity = vehicleTypeData.GetByIdAsync(transportSpecificServiceConfigPutDto.VehicleTypeId).GetAwaiter().GetResult() ?? throw new ArgumentException("找不到指定车型!");

                var org = transportSpecificServiceConfigData.GetEntityAsync(transportSpecificServiceConfigPutDto.TransportSpecificServiceConfigId).GetAwaiter().GetResult() ?? throw new ArgumentException("找不到要修改的记录");

                org.VehicleTypeId = vehicleTypeEntity.VehicleTypeId;
                org.VehicleTypeName = vehicleTypeEntity.VehicleTypeName;
                org.SpecificServiceType = transportSpecificServiceConfigPutDto.SpecificServiceType;
                org.ServiceTitle = transportSpecificServiceConfigPutDto.ServiceTitle;
                org.ShortName = transportSpecificServiceConfigPutDto.ShortName;
                org.HasRemark = transportSpecificServiceConfigPutDto.HasRemark;
                org.ServiceRemark = transportSpecificServiceConfigPutDto.ServiceRemark;
                org.ServiceAmount = transportSpecificServiceConfigPutDto.ServiceAmount;
                org.HasUnit = transportSpecificServiceConfigPutDto.HasUnit;
                org.ServiceUnit = transportSpecificServiceConfigPutDto.ServiceUnit;
                org.UpdateTime = DateTime.Now;
                org.EditAdminId = admin.AdminId;
                org.EditAdminName = admin.FullName;

                this.redisService.DeleteMultipleKey(key, false);
                return transportSpecificServiceConfigData.UpdateAsync(org).GetAwaiter().GetResult().As<TransportSpecificServiceConfigViewDto>();
            }
            else
            {
                throw new ArgumentException("代理商无权限");
            }
        }
        #endregion



        #region Delete
        public void Delete(long transportSpecificServiceConfigId, AdminDto admin)
        {
            if (admin.CompanyId == 1)
            {
                databaseContext.BeginTransaction();
                transportSpecificServiceConfigData.DeleteAsync(transportSpecificServiceConfigId);
                transportSpecificServiceConfigOverWriteData.DeleteByFatherIdAsync(transportSpecificServiceConfigId);
                databaseContext.Commit();

                this.redisService.DeleteMultipleKey(key, false);
                this.redisService.DeleteMultipleKey(keyView, false);
            }
            else
            {
                throw new Exception("代理人不能删除只能修改!");
            }

        }
        #endregion

        #region GetByTransportSpecificServiceConfigId
        public TransportSpecificServiceConfigDto GetById(long transportSpecificServiceConfigId)
        {
            var a = transportSpecificServiceConfigData.GetEntityAsync(transportSpecificServiceConfigId).GetAwaiter().GetResult();
            return a.As<TransportSpecificServiceConfigDto>();
        }
        #endregion


        #region GetList  获取运输特殊服务费配置记录--[分页]
        public PageableList<TransportSpecificServiceConfigDto> GetList(AdminDto admin, int? vehicleTypeId, string specificServiceType, string serviceTitle, int pageIndex, int pageSize)
        {
            int fCount = 0;
            IEnumerable<TransportSpecificServiceConfigEntity> fList;

            string cacheKey = this.redisService.GetKey(key, vehicleTypeId, specificServiceType, serviceTitle, pageIndex, pageSize);
            var result = this.redisService.TryGet(cacheKey, () =>
             {
                 (fList, fCount) = transportSpecificServiceConfigData.GetListAsync(vehicleTypeId, specificServiceType, serviceTitle, pageIndex, pageSize).GetAwaiter().GetResult();

                 return new PageableList<TransportSpecificServiceConfigDto>()
                 {
                     Count = fCount,
                     Items = fList.As<IEnumerable<TransportSpecificServiceConfigDto>>(),
                     PageIndex = pageIndex,
                     PageSize = pageSize,
                     ServerTime = DateTime.Now,
                 };
             }, TimeSpan.FromMinutes(30));
            return result;

        }
        #endregion

        #region GetOnlyList
        public List<TransportSpecificServiceConfigDto> GetOnlyList(int? vehicleTypeId, string specificServiceType, string serviceTitle)
        {
            string cacheKey = this.redisService.GetKey(key, vehicleTypeId, specificServiceType, serviceTitle);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                return transportSpecificServiceConfigData.GetOnlyListAsync(vehicleTypeId, specificServiceType, serviceTitle).GetAwaiter().GetResult().As<List<TransportSpecificServiceConfigDto>>();
            }, TimeSpan.FromMinutes(30));
            return result;
        }
        #endregion
    }
}